//
//  XMBagViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/29.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMTaskViewController.h"
#import <JXCategoryView/JXCategoryView.h>
#import "XMTaskListViewController.h"

@interface XMTaskViewController () <JXCategoryViewDelegate, JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray *taskArray;
@end


@implementation XMTaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
    [self taskData];
}

- (void)taskData {
    [MBProgressHUD showHUDActivityMsg:nil  toView:kWindow];
    NSMutableDictionary *dic = @{}.mutableCopy;
    [YHNetworkManager.sharedInstance postWithUrl:@"api/activity/task/config" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            self.taskArray = [result safe_objectForKey:@"task_mode"];
            NSMutableArray *titles = [NSMutableArray array];
            for (NSDictionary *dic in self.taskArray) {
                [titles addObject:dic[@"label"]];
            }
            self.categoryView.titles = titles;
            [self.categoryView reloadData];
        }
    }];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    self.navView.backgroundColor = kHEX(@"#F2F2F2");

    [self.navView insertSubview:self.categoryView atIndex:0];
    self.categoryView.listContainer = self.listContainerView;

    [self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.navView);
        make.centerY.equalTo(self.navTitleLa);
        make.height.mas_equalTo(44);
    }];

    [self.view addSubview:self.listContainerView];
    [self.listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, 0, 0));
    }];

    [self.listContainerView.scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
}

#pragma mark - JXCategoryListContainerViewDelegate
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.taskArray.count;
}

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    XMTaskListViewController *listView = [[XMTaskListViewController alloc] init];
    listView.type = [[self.taskArray safe_objectAtIndex:index] toInt:@"index"];
    return listView;
}

#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
}

#pragma mark - JXPagerMainTableViewGestureDelegate
- (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if (otherGestureRecognizer == self.categoryView.collectionView.panGestureRecognizer) {
        return NO;
    }
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}

#pragma mark - Getter
- (JXCategoryTitleView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 120, kNavBarHeight)];
        _categoryView.backgroundColor = kHEX(@"#F2F2F2");
        _categoryView.delegate = self;
        _categoryView.titleColor = kHEX(@"#1F1F1F");
        _categoryView.titleSelectedColor = kAppMainColor;
        _categoryView.titleFont = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];

        _categoryView.titleColorGradientEnabled = YES;
        _categoryView.titleLabelZoomEnabled = NO;
        _categoryView.titleLabelZoomScale = 1.0;
        _categoryView.titleLabelStrokeWidthEnabled = YES;

        JXCategoryIndicatorImageView *indicatorImageView = [[JXCategoryIndicatorImageView alloc] init];
        indicatorImageView.indicatorImageView.image = [UIImage imageNamed:@"bag_line"];
        indicatorImageView.indicatorImageViewSize = CGSizeMake(14, 6);
        indicatorImageView.verticalMargin = 5;
        _categoryView.indicators = @[indicatorImageView];

        _categoryView.titles = @[@"新手任务", @"日常任务"];
    }
    return _categoryView;
}

- (JXCategoryListContainerView *)listContainerView {
    if (!_listContainerView) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

@end
